/// <reference types="vitest" />

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { resolve } from 'path'
import DefineOptions from 'unplugin-vue-define-options/vite'
// import Components from 'unplugin-vue-components/vite'

const path = require("path");

const config = defineConfig({
  plugins: [vue(), vueJsx(),
    // @ts-ignore
    // Components({
    // // 指定组件位置，默认是src/components
    //   dirs: ["src/components"],
    //   extensions: ["vue"],
    //   deep: true,
    //   // 配置文件生成位置
    //   dts: "types/components.d.ts",
    // }),
    DefineOptions()
  ],
  target: "es2015",
  alias: {
    "@": resolve(__dirname, 'src'), // 路径别名
  },
  build: {
    outDir: path.resolve(__dirname, "./dist"),
    lib: {
      entry: path.resolve(__dirname, "./src/index.js"),
      name: "HaloEDesign",
    },
    rollupOptions: {
      context: "globalThis",
      preserveEntrySignatures: "strict",
      external: ["vue"],
      output: [
        {
          format: "umd",
          exports: "named",
          sourcemap: false,
          entryFileNames: "haloe.min.js",
          chunkFileNames: "[name].js",
          assetFileNames: "[name].[ext]",
          namespaceToStringTag: true,
          inlineDynamicImports: false,
          manualChunks: undefined,
          globals: { vue: "Vue" },
        },
        {
          format: "es",
          exports: "named",
          sourcemap: false,
          entryFileNames: "haloe.min.esm.js",
          chunkFileNames: "[name].js",
          assetFileNames: "[name].[ext]",
          namespaceToStringTag: true,
          inlineDynamicImports: false,
          manualChunks: undefined,
          globals: { vue: "Vue" },
        },
        {
          format: "es",
          exports: "named",
          sourcemap: false,
          // 配置打包根目录
          dir: 'dist/es',
          preserveModulesRoot: 'src',
          entryFileNames: '[name].js',
          preserveModules: true,
          chunkFileNames: "[name].js",
          assetFileNames: "[name].[ext]",
          namespaceToStringTag: true,
          inlineDynamicImports: false,
          manualChunks: undefined,
          globals: { vue: "Vue" },

        },
        {
          format: 'cjs',
          entryFileNames: '[name].js',
          preserveModules: true,
          dir: 'dist/lib',
          preserveModulesRoot: 'src',
          exports: "named",
          sourcemap: false,
          // 配置打包根目录
          chunkFileNames: "[name].js",
          assetFileNames: "[name].[ext]",
          namespaceToStringTag: true,
          inlineDynamicImports: false,
          manualChunks: undefined,
          globals: { vue: "Vue" },
        }
      ],
    },
  },
  resolve: {
    extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  },
  css: {
    preprocessorOptions: {
      less:{
        charset: false,
        additionalData: `@use "${path.resolve(__dirname, 'src/styles/index.less')}"`
      }
    }
  },
  define: {
    'process.env': { ...process.env }
  },
  test: {
    // 模拟dom环境
    environment: "happy-dom",
    coverage: {
      // 覆盖率提供者
      provider: "istanbul",
      reporter: ["text", "json", "html"],
      // 设置覆盖文件夹
      reportsDirectory: "./coverage",
      // 检查每个文件的阈值
      perFile: true,
      // 设置代码覆盖率阈值
      lines: 75,
      functions: 75,
      branches: 75,
      statements: 75,
    },
    // open: true,
    // ui:true,
    include: ["./src/components/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
  },
});

export default config;
